package sg.edu.nus.iss.team4ft.ui;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.HeadlessException;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.swing.BorderFactory;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import sg.edu.nus.iss.team4ft.exception.ComplaintException;
import sg.edu.nus.iss.team4ft.manager.ComplaintManager;
import sg.edu.nus.iss.team4ft.manager.CustomerAccountManager;
import sg.edu.nus.iss.team4ft.service.*;
import sg.edu.nus.iss.team4ft.util.DateUtil;


//complaint manager panel 
public class ComplaintPanel extends javax.swing.JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton viewComplaintButton;
	private JTextField accountText;
	private JComboBox complaintComboBox;
	private JComboBox statusComboBox;
	private JLabel complaintLabel;
	private JLabel statusLabel;
	private JLabel descriptionLabel;
	private JLabel accountLabel;
	private JButton updateCompalintButton;
	private JButton addComplaintButton;
	private JPanel centerPanel;
	private JPanel optionPanel;
	private String accountNumber;
	private String complaintNumber;
	private String option;
	private JScrollPane descriptionScrollPane;
	private CustomerAccountManager manager;
	private JLabel telephoneNumberLabel;
	private JTextField dateText;
	private JLabel dateLabel;
	private JButton cancelbutton;
	private JButton addbutton;
	private JPanel bottomPanel;
	private JLabel headingLabel;
	private JLabel accountHeading;
	private JComboBox telephoneNumberComboBox;
	private JTextField NRICText;
	private JTextField customerNameText;
	private JTextField customerIDText;
	private JLabel telephoneLabel;
	private JLabel NRICLabel;
	private JLabel customerNameLabel;
	private JLabel IDLabel;
	private JTextArea descriptionText;
	private ComplaintManager complaintManager;
	private Customer customer;
	private Account account;
	private String complaintID;
	private ArrayList<String> planslist;
	private String mobileVoiceNumber;
	private String digitalVoiceNumber;
	private ArrayList<CustomerAccount> customerAccounts;
	private ArrayList<String> accountNumbers;
	private Object[] acctNumbers;
	private ArrayList<String> complaintIDs;
	
	/**
	 * @throws Exception 
	 * @throws ClassNotFoundException 
	 * @throws ParseException 
	 * @throws IOException 
	*/
		
	public ComplaintPanel() {
		super();		
		customerAccounts = new ArrayList<CustomerAccount>();
		try {
			complaintManager = new ComplaintManager();			
			manager = new CustomerAccountManager();
			} catch (Exception e){
				JOptionPane.showMessageDialog(null, "Error Detected", "Error", JOptionPane.ERROR_MESSAGE);
				}
		initGUI();
		accountNumbers = new ArrayList<String>();
		}
	
	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(964, 518));
			this.setBackground(new java.awt.Color(232,230,231));
			this.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0,0,0)));
			{
				bottomPanel = new JPanel();
				AnchorLayout jPanel4Layout = new AnchorLayout();
				this.add(bottomPanel, new AnchorConstraint(827, 999, 1000, 212, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				bottomPanel.setLayout(jPanel4Layout);
				bottomPanel.setPreferredSize(new java.awt.Dimension(759, 90));
				bottomPanel.setBackground(new java.awt.Color(232,230,231));
				{
					cancelbutton = new JButton();
					bottomPanel.add(cancelbutton, new AnchorConstraint(627, 967, 883, 861, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					cancelbutton.setText("CANCEL");
					cancelbutton.setLayout(null);
					cancelbutton.setPreferredSize(new java.awt.Dimension(81, 23));
					cancelbutton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							cancelbuttonActionPerformed(evt);
						}
					});
				}
				{
					addbutton = new JButton();
					bottomPanel.add(addbutton, new AnchorConstraint(627, 835, 883, 729, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					addbutton.setText("ADD");
					addbutton.setLayout(null);
					addbutton.setPreferredSize(new java.awt.Dimension(81, 23));
					addbutton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								addbuttonActionPerformed(evt);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});

				}
			}
			{
				centerPanel = new JPanel();
				bottomPanel.setVisible(false);
				AnchorLayout jPanel4Layout = new AnchorLayout();
				this.add(centerPanel, new AnchorConstraint(51, 1002, 865, 194, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				centerPanel.setLayout(jPanel4Layout);
				centerPanel.setPreferredSize(new java.awt.Dimension(779, 422));
				centerPanel.setBackground(new java.awt.Color(232,230,231));

				{
					dateLabel = new JLabel();
					centerPanel.add(getDateText(), new AnchorConstraint(885, 954, 934, 755, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					centerPanel.add(getTelephoneNumberLabel(), new AnchorConstraint(889, 506, 939, 72, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					centerPanel.add(dateLabel, new AnchorConstraint(889, 755, 927, 570, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					dateLabel.setText("Date Created / Edited");
					dateLabel.setPreferredSize(new java.awt.Dimension(144, 16));
				}
				{
					headingLabel = new JLabel();
					centerPanel.add(headingLabel, new AnchorConstraint(1, 797, 117, 308, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					headingLabel.setText("ADD COMPLAINT");
					headingLabel.setPreferredSize(new java.awt.Dimension(381, 49));
					headingLabel.setFont(new java.awt.Font("Segoe UI",1,20));
				}
				{
					accountHeading = new JLabel();
					new AnchorLayout();
					centerPanel.add(accountHeading, new AnchorConstraint(168, 221, 225, 76, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					accountHeading.setText("Account Details:");
					accountHeading.setLayout(null);
					accountHeading.setPreferredSize(new java.awt.Dimension(113, 21));
					accountHeading.setFont(new java.awt.Font("Segoe UI",1,14));
				}
				{
					telephoneNumberComboBox = new JComboBox();
					ComboBoxModel telephoneComboBoxModel = 
							new DefaultComboBoxModel(
									new String[]{""});
					telephoneNumberComboBox.setModel(telephoneComboBoxModel);
					centerPanel.add(telephoneNumberComboBox, new AnchorConstraint(787, 507, 849, 263, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					telephoneNumberComboBox.setPreferredSize(new java.awt.Dimension(190, 26));
					telephoneNumberComboBox.addItemListener(new ItemListener() {
						public void itemStateChanged(ItemEvent evt) {
							try {
								telephoneNumberComboBoxItemStateChanged(evt);
							} catch (CloneNotSupportedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (ComplaintException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
					
					
				}
				{
					NRICText = new JTextField();
					centerPanel.add(NRICText, new AnchorConstraint(671, 502, 731, 262, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					NRICText.setPreferredSize(new java.awt.Dimension(187, 25));
					NRICText.setEditable(false);
				}
				{
					customerNameText = new JTextField();
					centerPanel.add(customerNameText, new AnchorConstraint(560, 506, 629, 263, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					customerNameText.setPreferredSize(new java.awt.Dimension(189, 29));
					customerNameText.setEditable(false);
				}
				{
					customerIDText = new JTextField();
					centerPanel.add(customerIDText, new AnchorConstraint(432, 506, 508, 263, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					customerIDText.setPreferredSize(new java.awt.Dimension(189, 32));
					customerIDText.setEditable(false);
				}
				{
					telephoneLabel = new JLabel();
					centerPanel.add(telephoneLabel, new AnchorConstraint(792, 239, 847, 72, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					telephoneLabel.setText("Telephone/Cable TV");
					telephoneLabel.setPreferredSize(new java.awt.Dimension(130, 23));
				}
				{
					NRICLabel = new JLabel();
					centerPanel.add(NRICLabel, new AnchorConstraint(688, 144, 731, 76, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					NRICLabel.setText("NRIC");
					NRICLabel.setPreferredSize(new java.awt.Dimension(53, 18));
				}
				{
					customerNameLabel = new JLabel();
					centerPanel.add(customerNameLabel, new AnchorConstraint(572, 211, 617, 76, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					customerNameLabel.setText("Customer Name");
					customerNameLabel.setPreferredSize(new java.awt.Dimension(105, 19));
				}
				{
					IDLabel = new JLabel();
					centerPanel.add(IDLabel, new AnchorConstraint(441, 185, 494, 76, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					IDLabel.setText("Customer ID");
					IDLabel.setPreferredSize(new java.awt.Dimension(85, 22));
				}
				{
					descriptionScrollPane = new JScrollPane();
					centerPanel.add(descriptionScrollPane, new AnchorConstraint(366, 958, 688, 751, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					descriptionScrollPane.setPreferredSize(new java.awt.Dimension(161, 136));

				}
				{				
					ComboBoxModel complaintComboBoxModel = 
							new DefaultComboBoxModel(
									new String[]{""});
					complaintComboBox = new JComboBox();
					centerPanel.add(complaintComboBox, new AnchorConstraint(282, 959, 351, 751, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					complaintComboBox.setModel(complaintComboBoxModel);
					complaintComboBox.setPreferredSize(new java.awt.Dimension(162, 27));
					complaintComboBox.addItemListener(new ItemListener() {
						public void itemStateChanged(ItemEvent evt) {
							try {
								complaintComboBoxItemStateChanged(evt);
							} catch (CloneNotSupportedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
				{
					ComboBoxModel statusComboBoxModel = 
							new DefaultComboBoxModel(
									new String[] { "open", "closed" });
					statusComboBox = new JComboBox();
					centerPanel.add(statusComboBox, new AnchorConstraint(750, 958, 828, 755, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					statusComboBox.setModel(statusComboBoxModel);
					statusComboBox.setPreferredSize(new java.awt.Dimension(158, 33));
				}
				{
					complaintLabel = new JLabel();
					centerPanel.add(complaintLabel, new AnchorConstraint(287, 746, 354, 571, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					complaintLabel.setText("Complaint Number");
				}
				{
					statusLabel = new JLabel();
					centerPanel.add(statusLabel, new AnchorConstraint(754, 734, 828, 571, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					statusLabel.setText("Complaint Status");
				}
				{
					accountText = new JTextField();
					centerPanel.add(accountText, new AnchorConstraint(303, 507, 367, 264, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					accountText.setText("ACC");
					accountText.setPreferredSize(new java.awt.Dimension(180, 24));
					accountText.setEditable(false);
					
				}
				{
					descriptionLabel = new JLabel();
					centerPanel.add(descriptionLabel, new AnchorConstraint(509, 689, 581, 572, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					descriptionLabel.setText("Description");
				}
				{
					accountLabel = new JLabel();
					new AnchorLayout();
					complaintLabel.setLayout(null);
					complaintLabel.setPreferredSize(new java.awt.Dimension(136, 26));
					statusLabel.setLayout(null);
					statusLabel.setPreferredSize(new java.awt.Dimension(127, 31));
					descriptionLabel.setLayout(null);
					descriptionLabel.setPreferredSize(new java.awt.Dimension(87, 27));
					centerPanel.add(accountLabel, new AnchorConstraint(306, 229, 367, 76, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					accountLabel.setText("Account Number");
					accountLabel.setLayout(null);
					accountLabel.setPreferredSize(new java.awt.Dimension(113, 23));
				}
				{
					descriptionText = new JTextArea();
					centerPanel.add(descriptionText, new AnchorConstraint(401, 954, 716, 751, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					descriptionScrollPane.setViewportView(descriptionText);
					descriptionText.setLineWrap(true);
					descriptionText.setPreferredSize(new java.awt.Dimension(158, 133));
					
				}
				centerPanel.setVisible(false);
			}

			{
				optionPanel = new JPanel();
				AnchorLayout jPanel2Layout = new AnchorLayout();
				this.add(optionPanel, new AnchorConstraint(0, 187, 1000, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				optionPanel.setLayout(jPanel2Layout);
				optionPanel.setPreferredSize(new java.awt.Dimension(180, 518));
				optionPanel.setBackground(new java.awt.Color(232,230,231));
				optionPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0,0,0)));
				{
					updateCompalintButton = new JButton();
					optionPanel.add(updateCompalintButton, new AnchorConstraint(257, 969, 305, 36, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					updateCompalintButton.setText("UPDATE COMPLAINT");
					updateCompalintButton.setLayout(null);
					updateCompalintButton.setPreferredSize(new java.awt.Dimension(168, 25));
					updateCompalintButton.setFont(new java.awt.Font("Segoe UI",1,12));
					updateCompalintButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								updateCompalintButtonActionPerformed(evt);
							} catch (HeadlessException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (CloneNotSupportedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});

				}
				{
					viewComplaintButton = new JButton();
					optionPanel.add(viewComplaintButton, new AnchorConstraint(186, 969, 236, 36, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					viewComplaintButton.setText("VIEW COMPLAINT");
					viewComplaintButton.setLayout(null);
					viewComplaintButton.setPreferredSize(new java.awt.Dimension(168, 26));
					viewComplaintButton.setFont(new java.awt.Font("Segoe UI",1,12));
					viewComplaintButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								viewComplaintButtonActionPerformed(evt);
							} catch (HeadlessException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} 
						}
					});

				}
				{
					addComplaintButton = new JButton();
					optionPanel.add(addComplaintButton, new AnchorConstraint(107, 969, 166, 36, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					addComplaintButton.setText("  ADD COMPLAINT");
					addComplaintButton.setLayout(null);
					addComplaintButton.setPreferredSize(new java.awt.Dimension(168, 31));
					addComplaintButton.setFont(new java.awt.Font("Segoe UI",1,12));
					addComplaintButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								addComplaintButtonActionPerformed(evt);
							} catch (HeadlessException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (NullPointerException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});

				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel getTelephoneNumberLabel() {
		if(telephoneNumberLabel == null) {
			telephoneNumberLabel = new JLabel();
			new AnchorLayout();
			telephoneNumberLabel.setLayout(null);
			telephoneNumberLabel.setPreferredSize(new java.awt.Dimension(338, 21));
		}
		return telephoneNumberLabel;
	}
	
	private JTextField getDateText() {
		if(dateText == null) {
			dateText = new JTextField();
			dateText.setPreferredSize(new java.awt.Dimension(155, 21));
		}
		return dateText;
	}
	
	
	/**
	 * @param evt
	 * @throws ParseException 
	 * @throws CloneNotSupportedException 
	 * @throws ClassNotFoundException 
	 * @throws Exception 
	 */
		
	private void addComplaintButtonActionPerformed(ActionEvent evt) throws ParseException, ClassNotFoundException, CloneNotSupportedException {
		try{
		accountNumber = getAccountNumberFromUser();
		descriptionText.setText("");
		dateText.setText("");
		statusComboBox.setSelectedIndex(0);
		resetComplaintItems();
		if(validateAccountNumber(accountNumber)){
		addComplaintPane();
		}
		}
		catch(IOException e){
			JOptionPane.showMessageDialog(centerPanel, "No Complaints exist in the System", "No Complaints in the System", JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	
	private void viewComplaintButtonActionPerformed(ActionEvent evt) throws HeadlessException, Exception {
		if (complaintManager.getListOfComplaints().size()==0) {
			JOptionPane.showMessageDialog(null,
					"No Complaints exist in the System", "Message",
					JOptionPane.ERROR_MESSAGE);
		} else {
		
			accountNumber = getAccountNumberFromUser();
			statusComboBox.setSelectedIndex(0);
			statusComboBox.setEnabled(false);
			resetComplaintItems();
			if (validateAccountNumber(accountNumber)) {
				option = "VIEW";
				telephoneNumberLabel.setText("");
				headingLabel.setText("VIEW COMPLAINT");
				account = manager.getCustomerAccountFromAccountNum(
						accountNumber).getAccount();
				complaintLabel.setVisible(true);
				complaintComboBox.setVisible(true);
				setParameters(account);
				addbutton.setVisible(false);
				descriptionText.setEditable(false);
				complaintComboBox.setEnabled(true);
				accountTextInputMethodTextChanged();

			}
		}
	}
	
	
	private void updateCompalintButtonActionPerformed(ActionEvent evt) throws HeadlessException, CloneNotSupportedException, Exception {
		if (complaintManager.getListOfComplaints().size()==0) {
			JOptionPane.showMessageDialog(null,
					"No Complaints exist in the System", "Message",
					JOptionPane.ERROR_MESSAGE);
		} else {
		accountNumber = getAccountNumberFromUser();
		statusComboBox.setSelectedIndex(0);
		resetComplaintItems();
		if(validateAccountNumber(accountNumber)){
			option = "UPDATE";
			telephoneNumberLabel.setText("");
			addbutton.setText("UPDATE");
			addbutton.setVisible(true);
			complaintLabel.setVisible(true);
			complaintComboBox.setVisible(true);
			headingLabel.setText("UPDATE COMPLAINT");
			account = manager.getCustomerAccountFromAccountNum(accountNumber).getAccount();
			setParameters(account);
			complaintComboBox.setEnabled(true);
			statusComboBox.setEnabled(true);
			descriptionText.setEditable(true);	
			accountTextInputMethodTextChanged();
	}	
	}
	}
	
	//Performs Adding or Updating functionality based on the selection made by the user
	private void addbuttonActionPerformed(ActionEvent evt) throws Exception {
		String plan = telephoneNumberComboBox.getSelectedItem().toString();
		String comID = "";
		String description = descriptionText.getText();
		if(addbutton.getText() == "UPDATE"){
			comID = complaintComboBox.getSelectedItem().toString();
		}
		account.getAccountNumber();
		if(!plan.equals("Select") && !description.equals("") && !comID.equals("Select")){
			
			if(addbutton.getText() == "ADD"){
				complaintID = complaintManager.generateComplaintID();
				ArrayList<String> allComplaints = new ArrayList<String>();
				allComplaints.add(complaintID);	
				complaintManager.addComplaint(complaintID , description, account.getAccountNumber(), plan);
				JOptionPane.showMessageDialog(centerPanel, "The Complaint ID is "+complaintID, "Complaint ID", JOptionPane.INFORMATION_MESSAGE);
				resetScreen();
				}else if(addbutton.getText() == "UPDATE"){
					complaintID = complaintComboBox.getSelectedItem().toString();
					Boolean updateStatus = false;
					int status = 0;
					status = complaintManager.getComplaint(complaintID).getComplaintStatus();
					if(!complaintID.equals("Select")){
						if(status == 1){
						
							JOptionPane.showMessageDialog(centerPanel, "Complaint already Closed", "Complaint Closed", JOptionPane.ERROR_MESSAGE);
							statusComboBox.setEnabled(false);
						} else{
							updateStatus = complaintManager.updateComplaint(complaintID, description, statusComboBox.getSelectedIndex(),account.getAccountNumber(), plan);
							}
						}else{
							JOptionPane.showMessageDialog(centerPanel, "Complaint Number is Invalid", "Complaint Closed", JOptionPane.ERROR_MESSAGE);
							}
					if(updateStatus){
						JOptionPane.showMessageDialog(centerPanel, "The Details of the Complaint ID  "+complaintID+"  was updated sucessfully", "complaintID", JOptionPane.INFORMATION_MESSAGE);
						}
					}
			resetScreen();
			}else{
				JOptionPane.showMessageDialog(centerPanel, "Details are Invalid", "Invalid Details", JOptionPane.ERROR_MESSAGE);
				}
		}
	
	
	
	//display the telephone number based on the selection in telephoneNumberComboBox
	private void telephoneNumberComboBoxItemStateChanged(ItemEvent evt) throws CloneNotSupportedException, ComplaintException {
		
		if(evt.getStateChange() == 1){
		descriptionText.setText("");
		dateText.setText("");
		String localTelephoneNumber = telephoneNumberComboBox.getSelectedItem().toString();
		resetComplaintItems();
		if(!localTelephoneNumber.equals("Select")){
			
			if(localTelephoneNumber.compareTo("Digital Voice")==0){
				telephoneNumberLabel.setText("Telephone Number:                                       " +digitalVoiceNumber);
				}else if(localTelephoneNumber.compareTo("Mobile Voice")==0){
					try {
						telephoneNumberLabel.setText("Telephone Number:                                       " +mobileVoiceNumber);
						} catch (Exception e) {
							e.printStackTrace();
							}
					}else{
						telephoneNumberLabel.setText("");
						}
			if(option.equals("VIEW") || (option.equals("UPDATE"))){
				setComplaintBasedOnTelephoneNumber();
				}
			}else{
				telephoneNumberLabel.setText("");
				}
		}
	}
		
	
	//Gets invoked when the Complaint Combo Box item is Selected or Deselected
	private void complaintComboBoxItemStateChanged(ItemEvent evt) throws CloneNotSupportedException {
		
		accountNumber = accountText.getText();
		complaintNumber = complaintComboBox.getSelectedItem().toString();
		if(!complaintNumber.equals("Select")){
			
			Complaint complaint = complaintManager.getComplaint(accountNumber,complaintNumber);
			complaint.getComplaintStatus();
			descriptionText.setText(complaint.getDescription());		
			statusComboBox.setSelectedIndex(complaint.getComplaintStatus());
			if(complaint.getComplaintStatus()==1 || option.equals("VIEW")){
				statusComboBox.setEnabled(false);
				}else{
					statusComboBox.setEnabled(true);
					}
			dateText.setText(complaint.getComplaintDate().toString());
			}else{
				descriptionText.setText("");
				dateText.setText("");
				}
		}
	
	
	private void cancelbuttonActionPerformed(ActionEvent evt) {
		System.out.println("cancelbutton.actionPerformed, event="+evt);
		resetScreen();
		}
	
	
	//This method asks input from user using a ComboBox and returns the selected item to the invoking method
	private String getAccountNumberFromUser() throws CloneNotSupportedException, ClassNotFoundException, IOException {
		manager.updateCustomerAccount();
		customerAccounts = manager.getAllCustomerAccount();
		accountNumbers = new ArrayList<String>();
		for(CustomerAccount custAcct: customerAccounts){
			if (custAcct.getCustomer().isActive()){
				accountNumbers.add(custAcct.getAccount().getAccountNumber());
				}
			}
		acctNumbers = new String[accountNumbers.size()];
		acctNumbers = accountNumbers.toArray();
		String localAccountNumber =  (String) JOptionPane.showInputDialog(centerPanel,  
                "Select the Account Number",  
                "Select Account", JOptionPane.PLAIN_MESSAGE,  
                null, acctNumbers, "");
		return localAccountNumber;
	}
	
	
	
	
/**This method gets the Account Number as Input and checks whether the Account Number exists and the if it exists,
 *   whether he is an Active Customer or not and returns a Boolean value to the invoking method
 * @throws CloneNotSupportedException 
**/
	private boolean validateAccountNumber(String accountNum) throws CloneNotSupportedException {
		String localAccountNumber = accountNum;
		Boolean isValid = false;
		if(!localAccountNumber.equals(null) && manager.getCustomerAccountFromAccountNum(localAccountNumber).getCustomer().isActive()){
			isValid = true;
			}
		return isValid;
		}

	
	
//This method adds the centerPanel and sets parameters required for performing the Add Complaint Operation
	private void addComplaintPane() throws  IOException, ParseException, ClassNotFoundException, CloneNotSupportedException{
		option = "ADD";
		telephoneNumberLabel.setText("Telephone Number:");
		headingLabel.setText("ADD COMPLAINT");
		addbutton.setVisible(true);
		addbutton.setText("ADD");
		complaintLabel.setVisible(false);		
		complaintComboBox.setVisible(false);
		CustomerAccount ca = manager.getCustomerAccountFromAccountNum(accountNumber);
		account = ca.getAccount();
		setParameters(account);
		descriptionText.setEditable(true);
		}

	
	//This method sets the Customer Parameters for the particular Account got as Input from the invoking method
	private void setParameters(Account account) {
		optionPanel.setVisible(true);
		centerPanel.setVisible(true);
		bottomPanel.setVisible(true);
		complaintComboBox.setEnabled(false);
		accountText.setText(account.getAccountNumber());
		statusComboBox.setEnabled(false);
		planslist = new ArrayList<String>();
		planslist.add("Select");
		planslist.addAll(selectPlanList());
		Object[] plans = new String[planslist.size()];
		plans = planslist.toArray();
		ComboBoxModel telephoneComboBoxModel = 
				new DefaultComboBoxModel(
						plans);
		telephoneNumberComboBox.setModel(telephoneComboBoxModel);
		
		try {
			customer = manager.getCustomerAccountFromAccountNum(account.getAccountNumber()).getCustomer();
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				}
		customerNameText.setText(customer.getName());
		customerIDText.setText(customer.getCustomerId());
		NRICText.setText(customer.getNRIC());
		dateText.setText((new Date()).toString());
		dateText.setEnabled(false);
		}

	
	private void accountTextInputMethodTextChanged() throws CloneNotSupportedException {
		
		accountNumber = accountText.getText();
		descriptionText.setText("");
		dateText.setText("");
		complaintComboBox.setSelectedItem("Select");
		getComplaintIDs(accountNumber);
		}
	
	
	//This method returns an Array List of Complaint IDs that exist in an Account Number
	private ArrayList<String> getComplaintIDs(String acc) throws CloneNotSupportedException {
		
		Complaint complaint;
		ArrayList<Complaint> tempList = new ArrayList<Complaint>();
		ArrayList<String> complaintIDs = new ArrayList<String>();
		tempList = complaintManager.getListOfComplaintsforAccountNum(acc);
		Iterator<Complaint> it = tempList.iterator();
		while(it.hasNext()){
			complaint = (Complaint)it.next();
			complaintIDs.add(complaint.getComplaintID());
			}
		return complaintIDs;
		}
	


	
	//This method resets the centerPanel and bottomPanel upon invoking
	private void resetScreen() {
		descriptionText.setText(null);
		centerPanel.setVisible(false);
		bottomPanel.setVisible(false);
		optionPanel.setVisible(true);
	}
	
	
	//This method returns and ArrayList of the Plans the Customer has subscribed with the System
	private ArrayList<String> selectPlanList() {
		ArrayList<String> planlist = new ArrayList<String>();
		if(account != null){
			ArrayList<Plan> plans = account.getPlans();
			for(int index = 0 ; index < plans.size(); index++){
				Plan plan = plans.get(index);
				if(plan instanceof MobileVoice){
					 mobileVoiceNumber = (((VoicePlan)plan).getTelephoneNumber().toString());
				}
				if(plan instanceof DigitalVoice){
					 digitalVoiceNumber =(((VoicePlan)plan).getTelephoneNumber().toString());
				}
					 
				String plan1 = plan.getServiceName(); 
				planlist.add(plan1);
				}
			}
		return planlist;
		}
	

	//This method sets all the complaint parameters based on the Plan the Customer is raising a Complaint for
	private void setComplaintBasedOnTelephoneNumber() throws CloneNotSupportedException, ComplaintException{
		
		accountText.getText();
		String plan = telephoneNumberComboBox.getSelectedItem().toString();
		ArrayList<Complaint> coms = new ArrayList<Complaint>();
		ArrayList<String> compIDs = new ArrayList<String>();
		compIDs.add("Select");
		coms = complaintManager.getListOfComplaints();
		for(int index = 0 ; index < coms.size(); index++){
			if(coms.get(index).getAccountNumber().compareTo(accountNumber)==0){
				if(coms.get(index).getPlan().compareTo(plan)==0){
					compIDs.add(coms.get(index).getComplaintID());
					}
				}
			}
		if(!compIDs.isEmpty()){
			setComboBoxKeys(compIDs);
			}
		if(compIDs.size() == 1){
			JOptionPane.showMessageDialog(centerPanel, "No Complaints registered for the selected Service", "No Complaints", JOptionPane.ERROR_MESSAGE);
			descriptionText.setText("");
			dateText.setText("");
			complaintComboBox.setSelectedIndex(0);
			}
	}
	

	//This method sets the Complaint Combo keys based on the complaint IDs given passed a ArrayList of Complaint IDs
	private void setComboBoxKeys(ArrayList<String> complaintIDs){
				
		Object [] keys = new String[complaintIDs.size()];
		keys = complaintIDs.toArray();
		ComboBoxModel complaintComboBoxModel = 
				new DefaultComboBoxModel(
						 keys);
		complaintComboBox.setModel(complaintComboBoxModel);
		}	

	
	//This method resets the Complaint Parameters when invoked
	private void resetComplaintItems() {
		complaintIDs = new ArrayList<String>();
		setComboBoxKeys(complaintIDs);
		}
	}
